#pragma once
#include <netinet/in.h>
#include <string_view>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
class unix_client{
public:
    unix_client(std::string_view unix_path){
        m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
        if (m_sock == -1){
            perror("创建socket失败\n");
            exit(-1);
        }

        memset(&m_sockaddr_server, 0, sizeof(m_sockaddr_server));
        m_sockaddr_server.sun_family = AF_UNIX;
        strcpy(m_sockaddr_server.sun_path, unix_path.data());

    }

    void connect(){
        if (::connect(m_sock, (sockaddr*)(&m_sockaddr_server), sizeof(m_sockaddr_server)) == -1){
            perror("连接服务器失败\n");
            exit(-1);
        }
    }


    void close(){
        if (m_sock != -1){
            ::close(m_sock);
            m_sock = -1;
        }
    }

    int get_sockfd(){
        return m_sock;
    }
private:
    int m_sock;
    sockaddr_un m_sockaddr_server;
};